﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace QQCommServer
{
    class Server
    {
        private TcpListener listenr;
        private Thread thread;
        private bool isClose = false;    //结束程序是退出循环 
        private Socket[] sock = new Socket[50];

        public Server()
        {
            Listener();
        }

        ~Server()
        {
            isClose = true;
            for (int i = 0; i < sock.Length; i++)
            {
                if (sock[i] != null)
                    sock[i].Close();
                listenr.Stop();
            }
        }

        /// <summary> 
        /// 开始监听端口 
        /// </summary> 
        public void Listener()
        {
            if (listenr == null)
                listenr = new TcpListener(IPAddress.Parse("127.0.0.1"), 5000);
            listenr.Start();
            if (thread == null)
                thread = new Thread(new ThreadStart(ReciverMessage));
            thread.IsBackground = true;
            thread.Start();
        }

        /// <summary> 
        /// 接收数据 
        /// </summary> 
        private void ReciverMessage()
        {
            int count; //可用套结字索引 
            count = GetCount();
            bool loop = true;
            if (count == -1)
                loop = false;
            if (loop)
            {
                while (true)
                {
                    // 判断是否退出循环 
                    if (isClose)
                        break;
                    sock[count] = listenr.AcceptSocket();
                    Thread t = new Thread(new ThreadStart(ReciverMessage));
                    t.IsBackground = true;
                    t.Start();
                    // 接受客户端数据 
                    while (true)
                    {
                        byte[] buffs = new byte[100];
                        if (sock[count].Connected)
                        {
                            try
                            {
                                sock[count].Receive(buffs);
                                string message = Encoding.Default.GetString(buffs);
                                Console.WriteLine(message);
                                SendToAll(buffs);
                            }
                            catch (Exception ex)
                            {

                            }
                        }
                    }
                }
            }
            Thread.CurrentThread.Abort();
        }

        // 得到可用套结字索引 
        private int GetCount()
        {
            for (int i = 0; i < sock.Length; i++)
                if (sock[i] == null)
                    return i;
            return -1;
        }

        // 发送消息给所有人 
        private void SendToAll(byte[] buff)
        {
            for (int i = 0; i < sock.Length; i++)
            {
                if (sock[i] != null)
                {
                    if (sock[i].Connected)
                    {
                        sock[i].Send(buff);
                    }
                }
            }
        }
    }
}

